package com.yymmarket.util;

import java.util.Properties;
import java.util.Enumeration;
import java.io.InputStream;
import java.io.IOException;

/**
 * 
 *  @author LeiHongyun  2010-10-27 09:49:54
 */
public class PropertiesUtil {
	private PropertiesUtil() {
	}

	private static Properties globalProperties = new Properties();

	/**
	 * Returns all InterMine properties
	 * 
	 * @return the global properties for InterMine
	 */
	public static Properties getProperties() {
		try {
			InputStream is = PropertiesUtil.class.getClassLoader()
					.getResourceAsStream("db.properties");

			if (is != null) {
				globalProperties.load(is);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return globalProperties;
	}

	/**
	 * Returns all InterMine properties
	 * 
	 * @return the global properties for InterMine
	 */
	public static Properties getProperties(String propertiesName) {
		try {
			InputStream is = PropertiesUtil.class.getClassLoader()
					.getResourceAsStream(propertiesName);

			if (is != null) {
				globalProperties.load(is);
			}

		} catch (IOException e) {
			// Do nothing
			e.printStackTrace();
		}
		return globalProperties;
	}

	/**
	 * Returns all Properties in props that begin with str
	 * 
	 * @param str
	 *            the String that the returned properties should start with
	 * @param props
	 *            the Properties to search through
	 * @return a Properties object containing the subset of props
	 */
	public static Properties getPropertiesStartingWith(String str,
			Properties props) {
		if (str == null) {
			throw new NullPointerException("str cannot be null");
		}
		if (props == null) {
			throw new NullPointerException("props cannot be null");
		}

		Properties subset = new Properties();
		Enumeration enumer = props.keys();
		while (enumer.hasMoreElements()) {
			String propertyName = (String) enumer.nextElement();
			if (propertyName.startsWith(str)) {
				subset.put(propertyName, props.get(propertyName));
			}
		}
		return subset;
	}

	/**
	 * Returns all global Properties that begin with str
	 * 
	 * @param str
	 *            the String that the returned properties should start with
	 * @return a Properties object containing the subset of the global
	 *         properties
	 */
	public static Properties getPropertiesStartingWith(String str) {
		return getPropertiesStartingWith(str, globalProperties);
	}

	/**
	 * Strips the give string off the keys of the given Properties. For example,
	 * database.name=production => name=production.
	 * 
	 * @param str
	 *            the String to strip off
	 * @param props
	 *            the Properties object to change
	 * @return a Properties object containing the same properties with the
	 *         initial string stripped off the keys
	 */
	public static Properties stripStart(String str, Properties props) {
		if (str == null) {
			throw new NullPointerException("str cannot be null");
		}
		if (props == null) {
			throw new NullPointerException("props cannot be null");
		}
		Properties ret = new Properties();
		Enumeration enumer = props.keys();
		while (enumer.hasMoreElements()) {
			String propertyName = (String) enumer.nextElement();
			if (propertyName.startsWith(str + ".")) {
				ret.put(propertyName.substring(str.length() + 1), props
						.get(propertyName));
			}
		}

		return ret;
	}
	
	
	public static void main(String[] args) {
		Properties props = PropertiesUtil.getProperties("error.properties");
		System.out.println(props.getProperty("errMessage"));
	}

}
